﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks; 
using Magicodes.Admin.EntityFrameworkCore;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;
using Abp.Linq.Extensions;
using Abp.AutoMapper;
using Microsoft.EntityFrameworkCore;
using Abp.Application.Services.Dto;
using Admin.Application.Custom.TimeLimits.Dto;
using System.Linq.Dynamic.Core;
using Magicodes.Admin.Custom.Fins;
using Magicodes.Admin;
using Abp.EntityFrameworkCore;
using Abp.Domain.Repositories;
using Magicodes.Admin.Custom.TimeLimits; 

namespace Admin.Application.Custom.Fins
{
    public class TimeLimitAppService : AppServiceBase, ITimeLimitAppService
    {  
        private readonly ITimeLimitRepository _timeLimitRepository; 
        public TimeLimitAppService(ITimeLimitRepository timeLimitRepository)
        {
            _timeLimitRepository = timeLimitRepository; 
        }
        public async Task<PagedResultDto<TimeLimitListDto>> GetPageList(GetTimeLimitPageListInput input)
        {
            var query = _timeLimitRepository.GetAll()
                .WhereIf(!string.IsNullOrWhiteSpace(input.SearchArea), s => s.Area == input.SearchArea)
                .WhereIf(input.SearchLimitDay > 0, s => s.LimitDay == input.SearchLimitDay);
            ;
            var count = await query.CountAsync();

            if (count == 0)
                return new PagedResultDto<TimeLimitListDto>();
            var list = query
              .OrderBy(input.Sorting)
              .PageBy(input)
              .ToList();

            var reList = list.MapTo<List<TimeLimitListDto>>();

            return new PagedResultDto<TimeLimitListDto>(
                count,
                reList
                );
        }
         
    }
}
